//
// Created by LiuYou on 2021/10/26.
//

#include <iostream>
#include <cstring>


static FILE* fp;


int main(int argc, char** argv) {
    int count{};
    while (true) {
        fp = fopen( "tmp", "r" );
        if (fp == nullptr) {
            fprintf( stderr, "fopen(): %s.\n", strerror(errno));
            break;
        }
        ++count;
    }
    atexit( [] {
        printf( "钩子函数运行。" );
        fclose( fp );
    } );

    printf( "count = %d\n", count );


    return 0;
}
/*
运行结果:
/home/liuyou/CLionProjects/LinuxEnvironmentProgramming2/cmake-build-debug/src/io/IO-fopen3
fopen(): Too many open files.
count = 1021

Process finished with exit code 139

 最多能打开 1021 个文件。

 一个进程默认打开了3个流: stdin stdout stderr。

 1021 + 3 = 1024

 所以一个进程能打开的文件个数是 1024 个。

liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15374
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
 # 这里可以看到一个进程可以打开的文件个数 限制在 1024 个。
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15374
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2$

 一个进程(在一个进程空间内)能够打开的文件个数最多是 1024 个。
 */

/*
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/cmake-build-debug/src/io$ ll
总用量 148
drwxrwxr-x 3 liuyou liuyou  4096 10月 26 13:48 ./
drwxrwxr-x 5 liuyou liuyou  4096 10月 26 10:40 ../
drwxrwxr-x 5 liuyou liuyou  4096 10月 26 13:44 CMakeFiles/
-rw-rw-r-- 1 liuyou liuyou  1157 10月 26 10:40 cmake_install.cmake
-rwxrwxr-x 1 liuyou liuyou 40224 10月 26 13:36 IO-fopen1*
-rwxrwxr-x 1 liuyou liuyou 40256 10月 26 13:39 IO-fopen2*
-rwxrwxr-x 1 liuyou liuyou 40248 10月 26 13:48 IO-fopen3*
-rw-rw-r-- 1 liuyou liuyou  9481 10月 26 13:44 Makefile
-rw-rw-r-- 1 liuyou liuyou     0 10月 26 13:39 tmp
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/cmake-build-debug/src/io$

 创建出来的 tmp 文件的属性:
 是 664 和 IO-fopen3.cpp 文件的权限一样。

liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/src/io$ ll
总用量 48
drwxrwxr-x 2 liuyou liuyou  4096 10月 26 13:44 ./
drwxrwxr-x 5 liuyou liuyou  4096 10月 26 10:40 ../
-rw-rw-r-- 1 liuyou liuyou 25469 10月 26 13:44 CMakeLists.txt
-rw-rw-r-- 1 liuyou liuyou   874 10月 26 13:38 IO-fopen1.cpp
-rw-rw-r-- 1 liuyou liuyou   782 10月 26 13:39 IO-fopen2.cpp
-rw-rw-r-- 1 liuyou liuyou  2780 10月 26 13:56 IO-fopen3.cpp
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/src/io$

 这个 tmp 文件的权限不是凭空出来的, 这个文件的权限是通过公式: 0666 & ~umask
 这样的表达式得出的。

liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2$ umask
0002
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2$

 0666 & ~0002
 0开头的是8进制数。

 0666 & ~0002 = 664

 umask值越大, 那么文件的权限就会将的越低, 这个文件被消掉的权限就越多。


 */